//
//  UITextView+YFTextView.m
//  ReitsChain
//
//  Created by 代佳宏 on 2017/10/3.
//  Copyright © 2017年 DaiJIaHong. All rights reserved.
//

#import "UITextView+YFTextView.h"
#import <objc/runtime.h>

static const char *jk_placeHolderTextView = "jk_placeHolderTextView";
static const void *JKTextViewInputLimitMaxLength = &JKTextViewInputLimitMaxLength;
static const void *JKTextViewInputLength = &JKTextViewInputLength;

@implementation UITextView (YFTextView)

- (UITextView *)jk_placeHolderTextView {
    return objc_getAssociatedObject(self, jk_placeHolderTextView);
}

- (void)setJk_placeHolderTextView:(UITextView *)placeHolderTextView {
    objc_setAssociatedObject(self, jk_placeHolderTextView, placeHolderTextView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)jk_addPlaceHolder:(NSString *)placeHolder {
    if (![self jk_placeHolderTextView]) {
        UITextView *textView = [[UITextView alloc] initWithFrame:self.bounds];
        textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        textView.font = self.font;
        textView.backgroundColor = [UIColor clearColor];
        textView.textColor = UIColorHex(0x999999);
        textView.userInteractionEnabled = NO;
        textView.text = placeHolder;
        [self addSubview:textView];
        [self setJk_placeHolderTextView:textView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:self];
        
    }
    self.jk_placeHolderTextView.text = placeHolder;
}
# pragma mark -
# pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(NSNotification *)noti {
    self.jk_placeHolderTextView.hidden = YES;
}

- (void)textViewDidEndEditing:(UITextView *)noti {
    if (self.text.length == 0) {
        self.jk_placeHolderTextView.hidden = NO;
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


// 字数限制
- (NSInteger)jk_maxLength {
    return [objc_getAssociatedObject(self, JKTextViewInputLimitMaxLength) integerValue];
}

- (void)setJk_maxLength:(NSInteger)maxLength {
    objc_setAssociatedObject(self, JKTextViewInputLimitMaxLength, @(maxLength), OBJC_ASSOCIATION_ASSIGN);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jk_textViewTextDidChange:)
                                                name:@"UITextViewTextDidChangeNotification" object:self];
    
}

- (NSInteger)inputLength {
    return [objc_getAssociatedObject(self, JKTextViewInputLength) integerValue];
}

- (void)setInputLength:(NSInteger)inputLength {
    objc_setAssociatedObject(self, JKTextViewInputLength, @(inputLength), OBJC_ASSOCIATION_ASSIGN);
}

- (void)jk_textViewTextDidChange:(NSNotification *)notification {
    NSString *toBeString = self.text;
    //获取高亮部分
    UITextRange *selectedRange = [self markedTextRange];
    UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
    
    //没有高亮选择的字，则对已输入的文字进行字数统计和限制
    //在iOS7下,position对象总是不为nil
    if ( (!position ||!selectedRange) && (self.jk_maxLength > 0 && toBeString.length > self.jk_maxLength)) {
        NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.jk_maxLength];
        if (rangeIndex.length == 1) {
            self.text = [toBeString substringToIndex:self.jk_maxLength];
        }
        else {
            NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.jk_maxLength)];
            NSInteger tmpLength;
            if (rangeRange.length > self.jk_maxLength) {
                tmpLength = rangeRange.length - rangeIndex.length;
            }
            else {
                tmpLength = rangeRange.length;
            }
            self.text = [toBeString substringWithRange:NSMakeRange(0, tmpLength)];
        }
    }
    self.inputLength = self.text.length;
}



- (instancetype)setupAttributedText:(NSString *)text{
    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    self.attributedText = attributedString;
    return self;
}
@end
